<?php

namespace App\Supports\Payment\Methods\Sandwap;

use Carbon\Carbon;
use App\Models\GoldCharge;
use App\Events\GoldChargePaidEvent;
use App\Supports\Payment\Methods\Method;
use App\Supports\Payment\Contracts\MethodCallback;

class CallbackGoldChargePay extends Method implements MethodCallback
{
	public function markAsPaid($charge = null, $data = null)
	{
		$charge->update([
			'paid_at' => Carbon::now(),
			'paid_method' => 'sandpay',
			'paid_number' => $data['orderCode']
		]);
		event(new GoldChargePaidEvent($charge));
		return response()->json(['respCode' => "000000"]);
	}
	
	public function callback($data = null)
	{
		$number = $this->getPaidNumber($data['tradeNo']);
		
		if (!$purchase = GoldCharge::where('number', $number)->first()) {
			return 'fail';
		}
		
		if ($purchase->isPaid()) {
			return 'fail';
		}
		
		return $this->markAsPaid($purchase, $data);
	}
}
